package com.audaque.springboot.foshanupload.web.filter;

/**
 * @author zgb
 * @desc ...
 * @date 2022-02-02 01:19:58
 */
import com.audaque.springboot.foshanupload.web.wrapper.ParameterServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;



/**
 * 参数加工过滤器（针对ajax、表单等请求）
 * 1.获取请求参数；2.对获取到的请求参数进行处理（解密、字符串替、请求参数分类截取等等）；3.把处理后的参数放回到请求列表里面
 *
 * @author zhaoheng
 *
 */
//@Component
@Slf4j
public class ParameterConvertFilter implements Filter {


    public String convert(String target) {
        log.debug("编码转换之前：" + target);
        try {
            String s = new String(target.trim().getBytes("ISO-8859-1"), "UTF-8");
            log.debug("编码转换之后：" + s);
            return s;
        } catch (UnsupportedEncodingException e) {
            return target;
        }
    }
    @Override
    public void destroy() {

    }
    private void logThread(){
        log.debug("ThreadId:" + Thread.currentThread().getId());
        log.debug("ThreadName:" + Thread.currentThread().getName());
    }
    @SuppressWarnings("unchecked")
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        String url0 = ((HttpServletRequest) request).getRequestURI();
        String url1 = ((HttpServletRequest) request).getContextPath();
        log.debug("完整地址：" + url0);
        log.debug("返回当前页面所在的应用的名字：" + url1);
        
        logThread();
        // 获取请求地址
        String url = ((HttpServletRequest) request).getRequestURI().substring(((HttpServletRequest) request).getContextPath().length());
        log.debug("controllerUrl：" + url);
        String method = ((HttpServletRequest) request).getMethod();
        log.debug("method：" + method);
        ParameterServletRequestWrapper requestWrapper1 = new ParameterServletRequestWrapper((HttpServletRequest) request);
        Map<String, String[]> parameterMap = requestWrapper1.getParameterMap();
        int size = parameterMap.size();
        if(size>0){
            parameterMap.forEach((k,v)->{
                log.debug("k:"+k);
                log.debug("v:"+v);
                Arrays.stream(v).forEach(e->this.convert(e));
            });
            //传递给目标servlet或jsp的实际上时包装器对象的引用，而不是原始的HttpServletRequest对象
            chain.doFilter(requestWrapper1, response);
            //放行后一定要return,否则有多个放行代码时会发起多个请求
            return;

        }


        chain.doFilter(request, response);
        //放行后一定要return,否则有多个放行代码时会发起多个请求
        return;

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }
}
